<?php

	Class Yomm_Devmod_Export
	{
		// database object
		private $db = null;

		private $devmod_id = null;
		
		public $log = array();
		
		public $name = null;
		
		public function __construct($db_obj)
		{
			$this->db = $db_obj;
		}
		
		public function start($devmod_id)
		{
			$this->devmod_id = $devmod_id;
			
			$obj = $this->db->select("dev_mods", array(
				"dev_sql.name" => "tablename",
				"dev_mods.name" => "name",
				"dev_mods.code" => "code",
				"dev_mods.theme" => "theme",
				"dev_mods.tpl_frontend_list" => "tpl_frontend_list",
				"dev_mods.tpl_frontend_add" => "tpl_frontend_add"
			), "LEFT JOIN (dev_sql) ON (dev_mods.dev_sql_id=dev_sql.id) WHERE dev_mods.id='{$this->devmod_id}'", false);

			$tbl_struct = $this->get_tbl_struct($obj->tablename);
			
			$tbl_data = $this->get_tbl_data($obj->tablename);
			
			$this->name = $obj->name;
			
			return $this->package($tbl_struct, $tbl_data, serialize(array(
				"sql_tablename" => $obj->tablename,
				"name" => $obj->name,
				"theme" => "$obj->theme",
				"code" => base64_encode($obj->code),
				"tpl_frontend_list" => $obj->tpl_frontend_list,
				"tpl_frontend_add" => $obj->tpl_frontend_add
			)));
		}
		
		public function getName()
		{
			return $this->name;
		}
		
		
		
		public function get_tbl_struct($table)
		{
			$sql_struct = 'CREATE TABLE IF NOT EXISTS `'.$table.'` (';
			
			$i = 1;
			
			$query = $this->db->query("SHOW COLUMNS FROM $table");
			
			
			while($data = mysqli_fetch_assoc($query))
			{
				$sql_struct .= '`'. $data['Field'] . '` ';
				
				switch($data['Type'])
				{
					case "int(11)" :
						$sql_struct .= 'INT ';
					break;
					
					case "varchar(255)" :
						$sql_struct .= 'VARCHAR (255) ';
					break;
					
					case  "text" : 
						$sql_struct .= 'TEXT ';
					break;
				}
				
				if($data['Null'] == "NO")
				{
					$sql_struct .= "NOT NULL ";
				}
				
				if($data['Key'] == "PRI")
				{
					$sql_struct .= "AUTO_INCREMENT PRIMARY KEY";
				}
				
				if($i != count($query))
				{
					$sql_struct .= ", ";
				}
				$i++;
			}
			
			$sql_struct .= ') ENGINE = MYISAM;';
			
			return $sql_struct;
		}
		
		public function get_tbl_data($table)
		{
			
			//$data = $this->db->select($table, "*", false, true);
			
			////////////////////////////////////////////////////////
			
			$num = $this->db->select($table, "*", false, true);
			
			if(count($num) == 0)
			{
				return null;	
			}
			
			$sql_data = "INSERT IGNORE INTO `$table` (";	
			
			$sql = $this->db->query("SHOW COLUMNS FROM `$table`");
			
			$i = 1;
			
			while($data = mysql_fetch_assoc($sql))
			{
				$sql_data .= "`".$data['Field']."`";
				
				if($i != $this->db->sql_numrows) $sql_data .= ", ";
				
				$i++;
			}
			
			$sql_data .= ") VALUES (";
			
			$obj = $this->db->select($table, "*", false, true);
			
			$i = 1;
			
			foreach($obj as $data)
			{
				$ii = 1;
				
				$data = (array) $data;
				
				foreach($data as $field => $content)
				{
					$sql_data .= "'$content'";
					
					if($ii != count($data)) $sql_data .= ", ";
					
					$ii++;
				}
				
				$sql_data .= ")";
				
				if($i != $this->db->sql_numrows) $sql_data .= ", (";
				
				$i++;
			}
			
			return $sql_data;
		}
		
		private function package($tbl_struct, $tbl_data, $sql_devmod)
		{
			$tbl_struct = base64_encode($tbl_struct);
			$tbl_data = base64_encode($tbl_data);
			$sql_devmod = base64_encode($sql_devmod);
			$pkg = base64_encode(trim("
				<TABLE_STRUCT>$tbl_struct</TABLE_STRUCT>
				<TABLE_DATA>$tbl_data</TABLE_DATA>
				<DEVMOD_SQL>$sql_devmod</DEVMOD_SQL>
			"));
			
			return $pkg;
		}
		
		private function log($text)
		{
			$this->log[] = array(
				'date' => date("H:i:s"),
				'text' => $text
			);
		}
		
		public function getLog()
		{
			$str = '<table border="1" class="color_table">';
			
			foreach($this->log as $item)
			{
				$str .= "<tr><td>" . $item['date'] . "</td><td>" . $item['text'] . "</td></tr>";
			}
			
			return $str . "</table>";
		}
		
	}